package com.manage.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.manage.modules.domain.TBaseStationStateYun;
import com.manage.modules.utils.DateUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.manage.modules.bo.TBaseSurfMinAddBo;
import com.manage.modules.bo.TBaseSurfMinQueryBo;
import com.manage.modules.bo.TBaseSurfMinEditBo;
import com.manage.modules.domain.TBaseSurfMin;
import com.manage.modules.mapper.TBaseSurfMinMapper;
import com.manage.modules.vo.TBaseSurfMinVo;
import com.manage.modules.service.ITBaseSurfMinService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 地面分钟要素Service业务层处理
 *
 * @author wx
 * @date 2021-09-08
 */
@Service
public class TBaseSurfMinServiceImpl extends ServiceImpl<TBaseSurfMinMapper, TBaseSurfMin> implements ITBaseSurfMinService {
    @Resource
    private TBaseSurfMinMapper surfMinMapper;

    @Override
    public void postOverSurfMin(List<TBaseSurfMin> surfMinList) {
        if (surfMinList == null || surfMinList.size() == 0) {
            return;
        }
        for (TBaseSurfMin surfMin : surfMinList) {
            LambdaQueryWrapper<TBaseSurfMin> lqw = Wrappers.lambdaQuery();
            lqw
                    .eq(true, TBaseSurfMin::getRequestNum, surfMin.getRequestNum())
                    .eq(true, TBaseSurfMin::getStationIdC, surfMin.getStationIdC());
            surfMin.setSurfId(getOne(lqw).getSurfId());
        }
        updateBatchById(surfMinList);
    }

    @Override
    public List<TBaseSurfMinVo> listMap() {
        // 查询每个站点地面要素的最新一条
        List<TBaseSurfMin> surfMinList = surfMinMapper.selectNewGroupByStationIdc();
        List<TBaseSurfMinVo> voList = new ArrayList<>();
        for (TBaseSurfMin surfMin : surfMinList) {
            // 一样得参数这里弄
            TBaseSurfMinVo vo = BeanUtil.toBean(surfMin, TBaseSurfMinVo.class);

            vo.setPre(validData(vo.getPre()));
            vo.setRhu(validData(vo.getRhu()));
            vo.setPrs(validData(vo.getPrs()));
            vo.setTem(validData(vo.getTem()));
            vo.setWinDInst(validData(vo.getWinDInst()));
            vo.setWinSInst(validData(vo.getWinSInst()));
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 对数据进行校验
     *
     * @return
     */
    private String validData(String str) {
        if ("999999".equals(str)) {
            str = "未测";
        }
        if ("999998".equals(str)) {
            str = "缺报";
        }
        return str;
    }

    @Override
    public TBaseSurfMinVo queryById(Long surfId) {
        TBaseSurfMin db = this.baseMapper.selectById(surfId);
        return BeanUtil.toBean(db, TBaseSurfMinVo.class);
    }

    @Override
    public List<TBaseSurfMinVo> queryList(TBaseSurfMinQueryBo bo) {
        LambdaQueryWrapper<TBaseSurfMin> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCreateTime() != null, TBaseSurfMin::getCreateTime, bo.getCreateTime());
        lqw.eq(StrUtil.isNotBlank(bo.getSurfType()), TBaseSurfMin::getSurfType, bo.getSurfType());
        lqw.eq(StrUtil.isNotBlank(bo.getStationIdC()), TBaseSurfMin::getStationIdC, bo.getStationIdC());
        lqw.orderByDesc(TBaseSurfMin::getSurfId);
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TBaseSurfMinVo> entity2Vo(Collection<TBaseSurfMin> collection) {
        List<TBaseSurfMinVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TBaseSurfMinVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TBaseSurfMin> page = (Page<TBaseSurfMin>) collection;
            Page<TBaseSurfMinVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TBaseSurfMinAddBo bo) {
        TBaseSurfMin add = BeanUtil.toBean(bo, TBaseSurfMin.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TBaseSurfMinEditBo bo) {
        TBaseSurfMin update = BeanUtil.toBean(bo, TBaseSurfMin.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TBaseSurfMin entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
